package com.siwen.config;

import com.siwen.api.ApiResult;
import com.siwen.common.ErrorCode;
import com.siwen.exception.UserException;
import com.siwen.util.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult<String> handleException(Exception e) {
        if (e instanceof UserException) {
            return ResultUtil.error(((UserException) e).getCode(),e.getMessage());

        }else {
           return ResultUtil.error(ErrorCode.UNKNOW_ERROR.getCode(),ErrorCode.UNKNOW_ERROR.getMessage());
        }
    }
}
